<?php
/**
 * Created by PhpStorm.
 * User: wjj
 * Date: 2016/11/1
 */

namespace app\logic;


use app\common\HttpCurl;
use app\common\PayUtil;
use app\models\CustomerService;
use app\models\Express;
use app\models\Goods;
use app\models\GoodsSku;
use app\models\Channel;
use app\models\Order;
use app\models\OrderGoods;
use app\models\Package;
use app\models\User;
use Pimple\Tests\Fixtures\Service;
use yii;


class OrderLogic
{
    public static $input_type_list = ['下拉框','输入框','单选项','多选项'];

    public function getOrderData($start_time,$end_time){
        $order_num = Order::find()->where("order_time>='$start_time' AND order_time<='$end_time'")->count();
        $not_yet_order_num = Order::find()->where("order_status=0 AND order_time>='$start_time' AND order_time<='$end_time'")->count();
        $yet_order_num = Order::find()->where("order_status>=1 AND order_time>='$start_time' AND order_time<='$end_time'")->count();

        $yet_order_money = Yii::$app->db
            ->createCommand("select sum(order_amount) as soa from tbl_order where order_status>=1 AND order_time>='$start_time' AND order_time<='$end_time' group by date_format(order_time, '%Y-%m-%d')")
            ->queryOne();
            /*->select('sum(order_amount) as ordersum')
            ->where("order_status=1 AND order_time>='$start_time' AND order_time<='$end_time'")->groupBy("date_format(order_time, $test)")->asArray()->one();*/

        $service_order_num = CustomerService::find()->where("service_time>='$start_time' AND service_time<='$end_time'")->count();
        $yet_order_money = isset($yet_order_money['soa'])?$yet_order_money['soa']:0;
        return [
            'order_num'=>$order_num,
            'not_yet_order_num'=>$not_yet_order_num,
            'yet_order_num'=>$yet_order_num,
            'yet_order_money'=>$yet_order_money,
            'service_order_num'=>$service_order_num,
        ];
    }

    public function getOrderDataChart($start_time,$end_time,$type=null){
        $yet_order_num = Order::find()->where("order_status>=1 AND order_time>='$start_time' AND order_time<='$end_time'")->count();
        if($type===null){
            $yet_order_money = Yii::$app->db
                ->createCommand("select sum(order_amount) as soa from tbl_order where order_status>=1 AND order_time>='$start_time' AND order_time<='$end_time' group by date_format(order_time, '%Y-%m-%d')")
                ->queryOne();
        }else{
            $yet_order_money = Yii::$app->db
                ->createCommand("select sum(order_amount) as soa from tbl_order where order_status>=1 AND order_time>='$start_time' AND order_time<='$end_time' group by date_format(order_time, '%Y-%m')")
                ->queryOne();
        }

        //$yet_order_money = Order::find()->where("order_status=1 AND order_time>='$start_time' AND order_time<='$end_time'")->sum("order_amount");
        //var_dump($yet_order_money);exit;
        $yet_order_money = isset($yet_order_money['soa'])?$yet_order_money['soa']:0;
        if($yet_order_num!=0)
            $money_per_customer = round($yet_order_money/$yet_order_num,2);
        else
            $money_per_customer = 0;
        return [
            'yet_order_num'=>$yet_order_num,
            'yet_order_money'=>$yet_order_money,
            'money_per_customer'=>$money_per_customer,
        ];
    }

    public function getOrder($query = [], $page = 1, $limit = 10)
    {
        /*$order = Order::find()
            ->select('o.*,u.mobile,count(DISTINCT og.goods_id) as goods_num,count(og.package_id) as package_num,c.channel_name')
            ->from('{{%order}} o')
            ->leftJoin('{{%user}} u', 'u.user_id=o.user_id')
            ->leftJoin('{{%order_goods}} og', 'og.order_id=o.order_id')
            ->leftJoin('{{%package}} p', 'p.order_id=o.order_id')
            ->leftJoin('{{%channel}} c', 'c.channel_id=o.channel_id')
            ->filterWhere($query)
            ->orderBy('order_id desc')
            ->offset(($page - 1) * $limit)->limit($limit);*/
        $order = OrderGoods::find()
            ->select('o.*,u.mobile,sum(DISTINCT og.buy_num) as goods_num,count(og.package_id) as package_num,c.channel_name,group_concat(p.express_no) as express_no,group_concat(p.package_status) as package_status')
            ->from('{{%order_goods}} og')
            ->leftJoin('{{%order}} o', 'og.order_id=o.order_id')
            ->leftJoin('{{%user}} u', 'u.user_id=o.user_id')
            ->leftJoin('{{%package}} p', 'p.order_id=o.order_id')
            ->leftJoin('{{%channel}} c', 'c.channel_id=o.channel_id')
            ->leftJoin('{{%goods}} g', 'g.yx_goods_id=og.yx_goods_id')
            ->where("1=1");

        if(isset($query['pay_type']) && $query['pay_type']!="全部"){
            $order->andWhere(['o.pay_type'=>$query['pay_type']]);
        }

        if(isset($query['pay_no']) && $query['pay_no']!=''){
            $order->andWhere(['o.pay_no'=>$query['pay_no']]);
        }

        if(isset($query['goods_no']) && $query['goods_no']!=''){
            $order->andWhere(['g.yx_goods_id'=>$query['goods_no']]);
        }
        if(isset($query['goods_name']) && $query['goods_name']!=''){
            $order->andWhere(['g.goods_name'=>$query['goods_name']]);
        }

        if(isset($query['order_no']) && $query['order_no']!=''){
            $order->andWhere(['o.order_no'=>$query['order_no']]);
        }
        if(isset($query['mobile']) && $query['mobile']!=''){
            $order->andWhere(['u.mobile'=>$query['mobile']]);
        }
        if(isset($query['order_status']) && $query['order_status']!=-1 && $query['order_status']!=6 && $query['order_status']!=7){
            $order->andWhere(['o.order_status'=>$query['order_status']]);
        }elseif(isset($query['order_status']) && $query['order_status']==6){
            $order->andWhere(['o.is_dai'=>1,'o.order_status'=>0]);
        }elseif(isset($query['order_status']) && $query['order_status']==7){
            $order->andWhere(['o.is_dai'=>1,'o.order_status'=>1]);
        }
        if(isset($query['channel_id']) && $query['channel_id']!=0){
            $order->andWhere(['c.channel_id'=>$query['channel_id']]);
        }
        if(isset($query['start_time']) && $query['start_time']!=''){
            $order->andWhere(['>=','o.order_time',date("Y-m-d 00:00:00", strtotime($query['start_time']))]);
        }
        if(isset($query['end_time']) && $query['end_time']!=''){
            $order->andWhere(['<=','o.order_time',date("Y-m-d 23:59:59", strtotime($query['end_time']))]);
        }
            $order->groupBy('og.order_id')
            ->orderBy('order_id desc');
        $count = $order->count();
        if($limit){
            $order->offset(($page - 1) * $limit)->limit($limit);
        }
        $lists = $order->asArray()->all();
        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $count,
                'page' => $page,
                'limit' => $limit,
                'url' => 'order/index',
                'query' => $query
            ]
        ];
        return $return;
    }

    public function getDaiOrder($query = [], $page = 1, $limit = 10)
    {
        /*$order = Order::find()
            ->select('o.*,u.mobile,count(DISTINCT og.goods_id) as goods_num,count(og.package_id) as package_num,c.channel_name')
            ->from('{{%order}} o')
            ->leftJoin('{{%user}} u', 'u.user_id=o.user_id')
            ->leftJoin('{{%order_goods}} og', 'og.order_id=o.order_id')
            ->leftJoin('{{%package}} p', 'p.order_id=o.order_id')
            ->leftJoin('{{%channel}} c', 'c.channel_id=o.channel_id')
            ->filterWhere($query)
            ->orderBy('order_id desc')
            ->offset(($page - 1) * $limit)->limit($limit);*/
        $order = OrderGoods::find()
            ->select('o.*,u.mobile')
            ->from('{{%order_goods}} og')
            ->leftJoin('{{%order}} o', 'og.order_id=o.order_id')
            ->leftJoin('{{%user}} u', 'u.user_id=o.user_id')
            ->leftJoin('{{%goods}} g', 'g.yx_goods_id=og.yx_goods_id')
            ->where(['o.is_dai'=>1]);



        if(isset($query['goods_no']) && $query['goods_no']!=''){
            $order->andWhere(['g.yx_goods_id'=>$query['goods_no']]);
        }
        if(isset($query['goods_name']) && $query['goods_name']!=''){
            $order->andWhere(['g.goods_name'=>$query['goods_name']]);
        }

        if(isset($query['order_no']) && $query['order_no']!=''){
            $order->andWhere(['o.order_no'=>$query['order_no']]);
        }
        if(isset($query['mobile']) && $query['mobile']!=''){
            $order->andWhere(['u.mobile'=>$query['mobile']]);
        }
        if(isset($query['order_status']) && $query['order_status']!=-1){
            $order->andWhere(['o.order_status'=>$query['order_status']]);
        }
        if(isset($query['start_time']) && $query['start_time']!=''){
            $order->andWhere(['>=','o.order_time',date("Y-m-d 00:00:00", strtotime($query['start_time']))]);
        }
        if(isset($query['end_time']) && $query['end_time']!=''){
            $order->andWhere(['<=','o.order_time',date("Y-m-d 23:59:59", strtotime($query['end_time']))]);
        }
        $order->groupBy('og.order_id')
            ->orderBy('order_id desc');
        $count = $order->count();
        if($limit){
            $order->offset(($page - 1) * $limit)->limit($limit);
        }
        $lists = $order->asArray()->all();

        $pu = new PayUtil();
        foreach($lists AS $key=>&$value){
            if($value['order_status']==0)
                $value['url'] = $pu->getUrl($value['order_no'],date('YmdHis',strtotime($value['order_time'])),$value['order_amount'],"中经代采购订单");
        }
        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $count,
                'page' => $page,
                'limit' => $limit,
                'url' => 'order/dai',
                'query' => $query
            ]
        ];
        return $return;
    }

    public function getOrderGoods($query = [], $page = 1, $limit = 10)
    {
        /*$order = Order::find()
            ->select('o.*,u.mobile,count(DISTINCT og.goods_id) as goods_num,count(og.package_id) as package_num,c.channel_name')
            ->from('{{%order}} o')
            ->leftJoin('{{%user}} u', 'u.user_id=o.user_id')
            ->leftJoin('{{%order_goods}} og', 'og.order_id=o.order_id')
            ->leftJoin('{{%package}} p', 'p.order_id=o.order_id')
            ->leftJoin('{{%channel}} c', 'c.channel_id=o.channel_id')
            ->filterWhere($query)
            ->orderBy('order_id desc')
            ->offset(($page - 1) * $limit)->limit($limit);*/
        $order = OrderGoods::find()
            ->select('o.order_no,o.pay_no,o.order_time,o.pay_time,o.update_time,o.order_status,o.pay_type,o.order_amount,u.mobile,og.*')
            ->from('{{%order_goods}} og')
            ->leftJoin('{{%order}} o', 'og.order_id=o.order_id')
            ->leftJoin('{{%user}} u', 'u.user_id=o.user_id')
            ->leftJoin('{{%package}} p', 'p.order_id=o.order_id')
            ->leftJoin('{{%channel}} c', 'c.channel_id=o.channel_id')
            ->leftJoin('{{%goods}} g', 'g.yx_goods_id=og.yx_goods_id')
            ->where("1=1");

        if(isset($query['pay_type']) && $query['pay_type']!="全部"){
            $order->andWhere(['o.pay_type'=>$query['pay_type']]);
        }

        if(isset($query['pay_no']) && $query['pay_no']!=''){
            $order->andWhere(['o.pay_no'=>$query['pay_no']]);
        }

        if(isset($query['goods_no']) && $query['goods_no']!=''){
            $order->andWhere(['g.yx_goods_id'=>$query['goods_no']]);
        }
        if(isset($query['goods_name']) && $query['goods_name']!=''){
            $order->andWhere(['g.goods_name'=>$query['goods_name']]);
        }

        if(isset($query['order_no']) && $query['order_no']!=''){
            $order->andWhere(['o.order_no'=>$query['order_no']]);
        }
        if(isset($query['mobile']) && $query['mobile']!=''){
            $order->andWhere(['u.mobile'=>$query['mobile']]);
        }
        if(isset($query['order_status']) && $query['order_status']!=-1){
            $order->andWhere(['o.order_status'=>$query['order_status']]);
        }
        if(isset($query['channel_id']) && $query['channel_id']!=0){
            $order->andWhere(['c.channel_id'=>$query['channel_id']]);
        }
        if(isset($query['start_time']) && $query['start_time']!=''){
            $order->andWhere(['>=','o.order_time',date("Y-m-d 00:00:00", strtotime($query['start_time']))]);
        }
        if(isset($query['end_time']) && $query['end_time']!=''){
            $order->andWhere(['<=','o.order_time',date("Y-m-d 23:59:59", strtotime($query['end_time']))]);
        }
        $order->orderBy('order_id desc');
        $count = $order->count();
        if($limit){
            $order->offset(($page - 1) * $limit)->limit($limit);
        }
        $lists = $order->asArray()->all();
        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $count,
                'page' => $page,
                'limit' => $limit,
                'url' => 'order/index',
                'query' => $query
            ]
        ];
        return $return;
    }



    public function getService($query = [], $page = 1, $limit = 10)
    {
        $order = CustomerService::find()
            ->select('cs.*,u.mobile,o.order_no')
            ->from('{{%customer_service}} cs')
            ->leftJoin('{{%order}} o', 'o.order_id=cs.order_id')
            ->leftJoin('{{%user}} u', 'u.user_id=cs.user_id')
            ->where("1=1");
        if(isset($query['order_no']) && $query['order_no']!=''){
            $order->andWhere(['cs.service_no'=>$query['order_no']]);
        }
        if(isset($query['mobile']) && $query['mobile']!=''){
            $order->andWhere(['u.mobile'=>$query['mobile']]);
        }
        if(isset($query['service_no']) && $query['service_no']!=''){
            $order->andWhere(['cs.service_no'=>$query['service_no']]);
        }
        if(isset($query['goods_name']) && $query['goods_name']!=''){
            $order->andWhere(['LIKE','cs.goods_name',$query['goods_name']]);
        }
        if(isset($query['yanxuan_status']) && $query['yanxuan_status']!=0){
            $order->andWhere(['cs.yanxuan_status'=>$query['yanxuan_status']]);
        }
        if(isset($query['is_refund']) && $query['is_refund']!=-1){
            $order->andWhere(['cs.is_refund'=>$query['is_refund']]);
        }
        if(isset($query['start_time']) && $query['start_time']!=''){
            $order->andWhere(['>=','cs.service_time',date("Y-m-d 00:00:00", strtotime($query['start_time']))]);
        }
        if(isset($query['end_time']) && $query['end_time']!=''){
            $order->andWhere(['<=','cs.service_time',date("Y-m-d 23:59:59", strtotime($query['end_time']))]);
        }

        $order->orderBy('service_id desc');
        $count = $order->count();
        if($limit)
            $order->offset(($page - 1) * $limit)->limit($limit);
        $lists = $order->asArray()->all();
        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $count,
                'page' => $page,
                'limit' => $limit,
                'url' => 'order/customer-service',
                'query' => $query
            ]
        ];
        return $return;
    }

    public function getById($id){
        return Order::find()->select('o.*,c.channel_name,u.mobile')
            ->from('{{%order}} o')
            ->leftJoin('{{%channel}} c', 'c.channel_id=o.channel_id')
            ->leftJoin('{{%user}} u', 'u.user_id=o.user_id')
            ->where(['o.order_id'=>$id])
            ->asArray()
            ->one();
    }

    public function getOrderGoodsById($id){
        return OrderGoods::find()
            ->select('og.*,g.yx_goods_id,s.yx_sku_id,p.package_status,s.sku_spec as f_sku_spec')
            ->from('{{%order_goods}} og')
            ->leftJoin('{{%goods}} g', 'g.goods_id=og.goods_id')
            ->leftJoin('{{%goods_sku}} s', 's.sku_id=og.sku_id')
            ->leftJoin('{{%package}} p', 'p.package_id=og.package_id')
            ->where(['og.order_id'=>$id])
            ->asArray()
            ->all();
    }

    public function getPackageById($id){
        return Express::find()->where(['order_id'=>$id])->asArray()->all();
    }

    public function getServiceById($id){
        return CustomerService::find()->select('cs.*,o.*,c.channel_name,u.mobile,g.yx_goods_id,s.yx_sku_id,s.sku_spec as f_sku_spec')
            ->from('{{%customer_service}} cs')
            ->leftJoin('{{%order}} o', 'o.order_id=cs.order_id')
            ->leftJoin('{{%channel}} c', 'c.channel_id=o.channel_id')
            ->leftJoin('{{%user}} u', 'u.user_id=o.user_id')
            ->leftJoin('{{%goods}} g', 'g.goods_id=cs.goods_id')
            ->leftJoin('{{%goods_sku}} s', 's.sku_id=cs.sku_id')
            ->where(['cs.service_id'=>$id])
            ->asArray()
            ->one();
    }

    public function refundById($id){
        $cs = CustomerService::findOne($id);
        $cs->is_refund = 1;
        $cs->save();
    }

    public function autoCancel($id){
         $order = Order::findOne($id);
         if($order->order_status==0){
             $order->order_status = 2;
             return $order->save();
         }
         return true;
    }

    public function autoReceived($id){
        $db=\Yii::$app->db;
        $transaction = $db->beginTransaction();
        $package = Package::findOne($id);
        $result1 = true;
        if($package->package_status==4){
            $package->package_status = 5;
            $result1 &= $package->save();
        }
        $allok = true;
        $packages = Package::find()->where(['order_id'=>$package->order_id])->asArray()->all();
        foreach($packages AS $key=>$value){
            if($value['package_status']!=5){
                $allok &= false;
            }
        }
        $result2 = true;
        $order = Order::findOne($package->order_id);
        if($allok){
            $order->update_time = date('Y-m-d');
            $order->order_status = 4;
            $result2 = $order->save();
        }
        if($result1 && $result2){
            $transaction->commit();
            return true;
        }else{
            $transaction->rollBack();
            return false;
        }

    }

    private function createOrderNo(){
        return date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
    }

    public function submitOrder($orderData){
        $user = User::find()->where(['mobile'=>$orderData[0]['A']])->asArray()->one();

        $db=\Yii::$app->db;
        $transaction = $db->beginTransaction();
        $order = new Order();
        $order_no = $this->createOrderNo();
        $order3 = Order::findOne(['order_no'=>$order_no]);
        while($order3){
            $order_no = $this->createOrderNo();
            $order3 = Order::findOne(['order_no'=>$order_no]);
        }
        $order->order_no = $order_no;
        $order->user_id = $user['user_id'];

        $order_time = date('Y-m-d H:i:s');
        $order->order_time = $order_time;
        $order->order_status = 0;
        $order->consignee = $orderData[0]['G'];
        $order->consignee_phone = $orderData[0]['B'];
        $order->consignee_province = $orderData[0]['C'];
        $order->consignee_city = $orderData[0]['D'];
        $order->consignee_district = $orderData[0]['E'];
        $order->consignee_address = $orderData[0]['F'];

        $goods_amount = $order->goods_amount = 0;
        $order->shipping_fee = 0;
        $channel_amount = $order->channel_amount = 0;
        $order->order_amount = 0;
        $order->is_dai = 1;




        $result1 = $order->save();
        $result2 = true;
        $result3 = true;
        foreach($orderData AS $ke=>$va){
            $skuids[] = $va['H'];
            $num[$va['H']] = $va['I'];
        }

        $goodsSku = GoodsSku::find()
            ->select('gs.*,g.yx_goods_id,g.goods_name')
            ->from('{{%goods_sku}} gs')
            ->leftJoin('{{%goods}} g', 'g.goods_id=gs.goods_id')
            ->where(['yx_sku_id'=>$skuids])->asArray()->all();
        foreach($goodsSku AS $key=>$value){
            $orderGoods = new OrderGoods();
            $orderGoods->order_id = $order->order_id;
            $orderGoods->yx_goods_id = $value['yx_goods_id'];
            $orderGoods->goods_id = $value['goods_id'];
            $orderGoods->goods_name = $value['goods_name'];
            $orderGoods->sku_id = $value['sku_id'];
            $orderGoods->yx_sku_id = $value['yx_sku_id'];
            $orderGoods->sku_pic = str_replace(Yii::$app->params['pic_domain'],"",$value['sku_pic']);
            $skuSpec = json_decode($value['sku_spec'],true);
            foreach($skuSpec AS $k=>$v){
                $f_sku_spec[] = $v['spec_value'];
            }
            $orderGoods->sku_spec = json_encode($f_sku_spec);
            $orderGoods->channel_price = $value['channel_price'];
            $orderGoods->sku_price = $value['sku_price'];
            $orderGoods->buy_num = $num[$value['yx_sku_id']];

            $result2 &= $orderGoods->save();
            $goods_amount += $value['sku_price']*$num[$value['yx_sku_id']];
            $channel_amount += $value['channel_price']*$num[$value['yx_sku_id']];
        }

        $order->goods_amount = $goods_amount;
        if($channel_amount<88){
            $order->shipping_fee = 10;
        }else{
            $order->shipping_fee = 0;
        }
        $order->channel_amount = $channel_amount;
        $order_amount = $goods_amount+$order->shipping_fee;
        $order->order_amount = $order_amount;
        $result3 = $order->save();

        if($result1 && $result2 && $result3){
            if(isset($orderData[0]['J'])){
                $order->order_status = 1;
                $time = date('Y-m-d H:i:s');
                $payMethod = $thirdPartyCode = "中经代付";
                $order->pay_type = $payMethod;
                $order->pay_time = $time;
                $order->pay_no = "";
                $order->update_time = $time;
                $result1 = $order->save();
                $order_id = $order->order_id;
                $skutemp = OrderGoods::find()->where(['order_id'=>$order_id])->asArray()->all();
                $goods_sale_volume = [];
                $result5 = true;
                foreach($skutemp AS $key=>$value){
                    if(isset($goods_sale_volume[$value['goods_id']])){
                        $goods_sale_volume[$value['goods_id']] += $value['buy_num'];
                    }else{
                        $goods_sale_volume[$value['goods_id']] = $value['buy_num'];
                    }
                    $sku[$key]['skuId'] = $value['yx_sku_id'];
                    $sku[$key]['productName'] = $value['goods_name'];
                    $sku[$key]['saleCount'] = $value['buy_num'];
                    $sku[$key]['originPrice'] = $value['channel_price'];
                    $sku[$key]['subtotalAmount'] = $value['channel_price']*$value['buy_num'];
                    $goodsSku = GoodsSku::findOne($value['sku_id']);
                    $goodsSku->sku_sales_volume += $value['buy_num'];
                    $result5 &= $goodsSku->save();
                }
                $result3 = true;
                foreach($goods_sale_volume AS $kk=>$vv){
                    $goods = Goods::findOne($kk);
                    $goods->sales_volume += $vv;
                    $result3 &= $goods->save();
                }
                $order = [
                    'orderId'=>$order_no,
                    'submitTime'=>$order->order_time,
                    'payTime'=>$order->pay_time,
                    'receiverName'=>$order->consignee,
                    'receiverPhone'=>$order->consignee_phone,
                    'receiverProvinceName'=>$order->consignee_province,
                    'receiverCityName'=>$order->consignee_city,
                    'receiverDistrictName'=>$order->consignee_district,
                    'receiverAddressDetail'=>$order->consignee_address,
                    'realPrice'=>$order->channel_amount+$order->shipping_fee,
                    'expFee'=>$order->shipping_fee,
                    'payMethod'=>$thirdPartyCode,                                //此处
                    'orderSkus'=>$sku
                ];
                $httpCurl = new HttpCurl();
                $result = $httpCurl->rollpost("yanxuan.order.paid.create",['order'=>json_encode($order)]);
                $result = json_decode($result,true);
                $result2 = true;
                $result4 = true;
                foreach($result['orderPackages'] AS $key=>$value){
                    $package = new Package();
                    $package->yx_package_id = $value['packageId'];
                    $package->order_id = $order_id;
                    $package->package_status = 2;
                    $result2 &= $package->save();
                    $skus = $value['orderSkus'];
                    foreach($skus AS $kk=>$vv){
                        $og = OrderGoods::findOne(['order_id'=>$order_id,'yx_sku_id'=>$vv['skuId']]);
                        $og->package_id = $package->package_id;
                        $result4 &= $og->save();
                    }
                }
                if($result1 && $result2 && $result3 && $result4 && $result5){
                    $transaction->commit();
                    return true;
                }else{
                    $transaction->rollBack();
                    return false;
                }
            }






            $transaction->commit();
            return true;
        }else{
            $transaction->rollBack();
            return false;
        }
    }






}
